<template>
	<!-- 签到积分 -->
	<!-- 周一航 2023-4-6 -->
	<view>
		<view class="body">
			<image src="http://101.43.223.224:8765/image/getImageById?id=803" mode="" style="width: 100%;height: 300rpx;"></image>
			<view class="my_integral">
				我的积分：{{userIntegral}}（可抵{{userIntegral/100}}元）
			</view>
			<view class="sign_in" @click="signIn()">
				{{signInState}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 用户积分
				userIntegral: 0,
				// 是否签到
				signInState: "立即签到",
				// 用户信息
				userInfo: {},
			}
		},
		onLoad() {
			// 查询我的积分
			this.selectUserIntegral();
			// 查询用户是否今天签到
			this.selectSignInState();
		},
		methods: {
			// 签到
			signIn(){
				this.$myRequest({
					// method: 'POST',
					url: '/newDaily/signIn',
					data: {
						userId: this.userInfo.id,
						integral: this.userIntegral
					}
				}).then(res => {
					if(res.data.flag){
						uni.showToast({
							title: '签到成功',
							icon: 'success',
							duration: 2000
						})
						// 查询我的积分
						this.selectUserIntegral();
						// 查询用户是否今天签到
						this.selectSignInState();
					}else{
						uni.showToast({
							title: '签到失败',
							icon: 'error',
							duration: 2000
						})
					}
				})
			},
			// 查询用户积分
			selectUserIntegral(){
				this.userInfo = wx.getStorageSync('user')
				this.$myRequest({
					url: '/newDaily/selectUserIntegral',
					data: {
						userId:this.userInfo.id
					}
				}).then(res => {
					if(res.data.flag){
						this.userIntegral = res.data.data.integral
					}
				})
			},
			// 查询用户是否签到
			selectSignInState(){
				this.$myRequest({
					url: '/newDaily/selectSignInState',
					data: {
						userId:this.userInfo.id
					}
				}).then(res => {
					if(res.data.flag){
						this.signInState = "已签到"
					}else{
						this.signInState = "立即签到"
					}
				})
			},
		}
	}
</script>

<style>
.body{
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	background: #C9D9DB;
}
.my_integral{
	width: 600rpx;
	height: 70rpx;
	padding-top: 30rpx;
	margin: 60rpx auto 300rpx;
	background-color: rgb(78, 85, 163, 0.5);
	border-radius: 6px;
	text-align: center;
	color: #fff;
}
.sign_in{
	width: 400rpx;
	height: 400rpx;
	margin: 0 auto;
	background-color: rgb(137, 181, 228, 0.8);
	color: white;
	border-radius: 50%;
	font-size: 42rpx;
	text-align: center;
	padding-top: 170rpx;
	box-sizing: border-box;
}
</style>
